//
//  HomeSideViewController.m
//  SHHCPhone
//
//  Created by sheng long on 14-6-19.
//  Copyright (c) 2014年 sheng long. All rights reserved.
//

#import "HomeSideViewController.h"
#import "UIViewController+MMDrawerController.h"
#import "LoginViewController.h"
#import "HistoryDataViewController.h"
#import "DailyDietViewController.h"
#import "MoveGuideViewController.h"
#import "BodyCompositionViewController.h"
#import "BabyCounterViewController.h"
#import "HealthViewController.h"
#import "FeedBackViewController.h"
#import "HomeMainViewController.h"
#import "ConsultViewController.h"
#import "SportsGuideViewController.h"
#import "FetusViewController.h"
#import "BodyIngredientViewController.h"
#import "HotAskViewController.h"

@interface HomeSideViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    //显示的数据
    NSArray *dataArray;
    NSArray *_vcArray;
    
    //初始化显示的数据
    NSArray *allDataArray;
    NSArray *allVcArray;
    
    //选中的cell
    NSInteger selecCell;
    
    //头像和昵称
    UIImageView *headImage;
    UILabel *userLabel;
    
    //表格
    UITableView *mainTable;
    
    //
    UINavigationController *currentNav;
}
@end

@implementation HomeSideViewController

@synthesize vcArray=_vcArray;

-(void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateUserMsg) name:APP_UPDATE_USER_MSG object:nil];
    
    allDataArray = [[NSArray alloc] initWithObjects:@[@"首页-侧边-首页icon",@"首页"],@[@"首页-侧边-历史数据icon",@"历史数据"],@[@"首页-侧边-体成分",@"人体成分"],@[@"首页-侧边-每日食谱icon",@"每日食谱"],@[@"首页-侧边-运动指导icon",@"运动指导"],@[@"首页-侧边-胎儿计算器icon",@"胎儿计算器"],@[@"首页-侧边-bady计算器icon",@"Baby计算器"],@[@"首页-侧边-健康动态",@"健康动态"],@[@"首页-侧边-营养咨询icon",@"营养咨询"],@[@"首页-侧边-答题icon",@"最热提问"],nil];
        
    UIViewController *vc = ((UINavigationController*)self.mm_drawerController.centerViewController).topViewController;
    allVcArray = @[vc,[[HistoryDataViewController alloc]init],[[BodyIngredientViewController alloc] init],[[DailyDietViewController alloc]init],[[SportsGuideViewController alloc]init],[[FetusViewController alloc]init],[[BabyCounterViewController alloc]init],[[HealthViewController alloc]init],[[ConsultViewController alloc]init],[[HotAskViewController alloc]init]];
    
    [navigationBarView removeFromSuperview];
    [self.view setBackgroundColor:GET_COLOR(@"373540")];
    
    headImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"首页-侧边-用户头像框-默认"]];
    [headImage sizeToFit];
    [headImage.layer setCornerRadius:MIN(headImage.frame.size.width, headImage.frame.size.height)/2];
    [headImage.layer setMasksToBounds:YES];
    [headImage setImageWithURL:[NSURL URLWithString:APP_USER_MSG.headimg] placeholderImage:[UIImage imageNamed:@"首页-侧边-用户头像框-默认"]];
    [headImage setCenter:CGPointMake(HOME_SIDE_WIDTH/2, headImage.frame.size.height/2+NAVIGATION_ORY+10)];
    [self.view addSubview:headImage];
    
    userLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, headImage.frame.size.height+headImage.frame.origin.y+8, HOME_SIDE_WIDTH, 15)];
    [userLabel setText:APP_USER_MSG.nickname];
    [userLabel setTextColor:GET_COLOR(@"aaa9ac")];
    [userLabel setBackgroundColor:[UIColor clearColor]];
    [userLabel setTextAlignment:NSTextAlignmentCenter];
    [userLabel setFont:[UIFont boldSystemFontOfSize:14]];
    [self.view addSubview:userLabel];
    
    mainTable = [[UITableView alloc] initWithFrame:CGRectMake(0, userLabel.frame.origin.y+userLabel.frame.size.height+20, HOME_SIDE_WIDTH, CURRENT_CONTENT_HEIGHT-(userLabel.frame.origin.y+userLabel.frame.size.height+20))];
    [self.view addSubview:mainTable];
    [mainTable setBounces:NO];
    [mainTable setBackgroundColor:[UIColor clearColor]];
    [mainTable setSeparatorColor:[UIColor clearColor]];
    [mainTable setDelegate:self];
    [mainTable setDataSource:self];
    
    [self showCell];
    
    AppDelegate_A.leftVCArray = allVcArray;
}

-(void)updateUserMsg
{
    [headImage setImageWithURL:[NSURL URLWithString:APP_USER_MSG.headimg] placeholderImage:[UIImage imageNamed:@"首页-侧边-用户头像框-默认"]];
    [userLabel setText:APP_USER_MSG.nickname];
    [self showCell];
}

-(void)showCell
{
    CLog(@"NSStringFromClass===%@===%@",self.mm_drawerController.centerViewController,self.mm_drawerController);
    
    NSMutableArray *array1 = [NSMutableArray arrayWithCapacity:1];
    NSMutableArray *array2 = [NSMutableArray arrayWithCapacity:1];
    switch ([APP_USER_MSG.pregnancy intValue]) {
        case 1:
        {
            //孕前
            //不需要显示婴儿计算器和胎儿计算器
            for (int i = 0 ; i < allVcArray.count; i++) {
                UIViewController *vc = [allVcArray objectAtIndex:i];
                if (!([vc isKindOfClass:[BabyCounterViewController class]] || [vc isKindOfClass:[FetusViewController class]])) {
                    [array1 addObject:[allDataArray objectAtIndex:i]];
                    [array2 addObject:vc];
                }
            }
        }
            break;
        case 2:
        {
            //孕中
            NSDate *lastDate = [z_DateTime dencodeTime:APP_USER_MSG.last_menstrual format:APP_SELECTPICKERVIEW_DATE_FORMAT];
            int total = ([[NSDate date] timeIntervalSinceDate:lastDate])/3600/24;
            if (total <= 250) {
                //不需要显示婴儿计算器
                for (int i = 0 ; i < allVcArray.count; i++) {
                    UIViewController *vc = [allVcArray objectAtIndex:i];
                    if (![vc isKindOfClass:[BabyCounterViewController class]]) {
                        [array1 addObject:[allDataArray objectAtIndex:i]];
                        [array2 addObject:vc];
                    }
                }
            }else if (total <= 360){
                //全部都有
                for (int i = 0 ; i < allVcArray.count; i++) {
                    UIViewController *vc = [allVcArray objectAtIndex:i];
                    [array1 addObject:[allDataArray objectAtIndex:i]];
                    [array2 addObject:vc];
                }
            }else{
                //不需要显示胎儿计算器
                for (int i = 0 ; i < allVcArray.count; i++) {
                    UIViewController *vc = [allVcArray objectAtIndex:i];
                    if (![vc isKindOfClass:[FetusViewController class]]) {
                        [array1 addObject:[allDataArray objectAtIndex:i]];
                        [array2 addObject:vc];
                    }
                }
            }
        }
            break;
        case 3:
        {
            //孕后
            //不需要显示胎儿计算器
            for (int i = 0 ; i < allVcArray.count; i++) {
                UIViewController *vc = [allVcArray objectAtIndex:i];
                if (![vc isKindOfClass:[FetusViewController class]]) {
                    [array1 addObject:[allDataArray objectAtIndex:i]];
                    [array2 addObject:vc];
                }
            }
        }
            break;
        default:
            break;
    }
    
    if (_vcArray) {
        NSObject *old = [_vcArray objectAtIndex:selecCell];
        if ([array2 containsObject:old]) {
            selecCell = [array2 indexOfObject:old];
        }else{
            selecCell = 0;
            NSMutableArray *array = [NSMutableArray arrayWithArray:currentNav.viewControllers];
            [array insertObject:[allVcArray objectAtIndex:0] atIndex:0];
            currentNav.viewControllers = array;
        }
    }
    
    dataArray = array1;
    _vcArray = array2;
    [mainTable reloadData];
}

#pragma mark - UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * cellId = @"UITableViewCell";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
        [cell setBackgroundColor:[UIColor clearColor]];
        [cell.textLabel setFont:[UIFont boldSystemFontOfSize:13]];
        
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, cell.frame.size.height-1, cell.frame.size.width, 1)];
        [view setBackgroundColor:GET_COLOR(@"4b4a54")];
        [cell addSubview:view];
        
        UIView *view1 = [[UIView alloc] initWithFrame:cell.bounds];
        [view1 setBackgroundColor:GET_COLOR(@"d9d9d9")];
        [cell setSelectedBackgroundView:view1];
    }
    NSArray *array = [dataArray objectAtIndex:indexPath.row];
    [cell.textLabel setText:[array objectAtIndex:1]];
    NSString *imageName = nil;
    if (selecCell == indexPath.row) {
        imageName = [NSString stringWithFormat:@"%@-点击",[array objectAtIndex:0]];
        [cell.textLabel setTextColor:GET_COLOR(@"fd77a8")];
    }else{
        imageName = [array objectAtIndex:0];
        [cell.textLabel setTextColor:GET_COLOR(@"aaa9ac")];
    }
    [cell.imageView setImage:[UIImage imageNamed:imageName]];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    selecCell = indexPath.row;
    [tableView reloadData];
    
    UIViewController *vc = [_vcArray objectAtIndex:indexPath.row];
    if ([vc isKindOfClass:[DailyDietViewController class]] || [vc isKindOfClass:[SportsGuideViewController class]] || [vc isKindOfClass:[ConsultViewController class]] || [vc isKindOfClass:[BodyIngredientViewController class]] || [vc isKindOfClass:[HotAskViewController class]] ) {
        [((BaseViewController*)vc) setShowBackBtn:NO];
    }
    
    if ([vc isKindOfClass:[BodyIngredientViewController class]]) {
        [((BodyIngredientViewController*)vc) setVcArray:AppDelegate_A.leftVCArray];
    }
    
    UINavigationController * nav = [[UINavigationController alloc] initWithRootViewController:[_vcArray objectAtIndex:indexPath.row]];
    [nav.navigationBar setHidden:YES];
    [self.mm_drawerController setCenterViewController:nav withCloseAnimation:YES completion:nil];
    
    currentNav = nav;
}

@end
